package edu.uwosh.cs342.project3;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class Parse {

	String quizName;
	int numberOfQuestions;
	int[] questionTypeArray;
	String[] questionStatements;
	String[] questionAnswers;
	String[][] questionOptions;
	
	public Parse(File f) throws JDOMException, IOException{

		//if(f.exists())
			//System.out.println("DocType:  works");
		
		//  Get document root from file
		Document doc = getDocumentRoot(f);
		Element root = doc.getRootElement();
		
		//  Get the quiz name and number of questions
		quizName = root.getAttributeValue("name");
		numberOfQuestions = Integer.parseInt(root.getAttributeValue("numQuestions"));
		
		//  Initialize arrays
		List list = root.getChildren();	
		initializeArrays(list);
	}
	
	public void initializeArrays(List list){
		//  Array of questionTypes
		questionTypeArray = makeQuestionTypesArray(list);
		
		//  Array of questionsStatements
		questionStatements = makeQuestionStatements(list);

		//  Array of questionAnswers
		questionAnswers = makeQuestionAnswersArray(list);
		
		//  Array of questionOptions if any
		questionOptions = makeQuestionOptions(list);
	}
	
	public Document getDocumentRoot(File f) throws JDOMException, IOException{
		SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(f);
		return doc;
	}
	
	public String getName(){
		return quizName;
	}

	public int getNumberOfQuestions(){
		return numberOfQuestions;
	}
	
	public int[] getQuestionTypeArray(){
		return questionTypeArray;
	}
	
	public String[] getQuestionStatements(){
		return questionStatements;
	}
	
	public String[] getQuestionAnswers(){
		return questionAnswers;
	}
	
	public String[][] getQuestionOptions(){
		return questionOptions;
	}
	
	private int[] makeQuestionTypesArray(List list){
		int[] questionArray = new int[list.size() + 1];
		int j = 0;
		for(Iterator i = list.iterator(); i.hasNext();){
			Element e = (Element) i.next();
			
			if(e.getAttributeValue("format").equals("mc"))
				questionArray[j] = 0;
			else if(e.getAttributeValue("format").equals("fb"))
				questionArray[j] = 1;
			else
				questionArray[j] = 2;
			j++;
		}
		questionArray[list.size()] = 3;
		return questionArray;
	}
	
	private String[] makeQuestionStatements(List pList){
		String[] qStatements = new String[pList.size()];
		for(int i=0; i < pList.size(); i++){
			Element a = (Element) pList.get(i);
			List exampleList = a.getChildren();
			Element statement = (Element) exampleList.get(0);
			qStatements[i] = statement.getValue();
		}
		return qStatements;
	}
	
	private String[] makeQuestionAnswersArray(List pList){
		String[] qStatements = new String[pList.size()];
		for(int i=0; i < pList.size(); i++){
			Element a = (Element) pList.get(i);
			List exampleList = a.getChildren();
			Element statement = (Element) exampleList.get(1);
			qStatements[i] = statement.getValue();
		}
		return qStatements;
	}

	private String[][] makeQuestionOptions(List pList){
		String[][] qStatements = new String[pList.size()][3];
		for(int i=0; i < pList.size(); i++){
			Element a = (Element) pList.get(i);
			List exampleList = a.getChildren();
			if(exampleList.size() > 4){
				Element statement1 = (Element) exampleList.get(2);
				Element statement2 = (Element) exampleList.get(3);
				Element statement3 = (Element) exampleList.get(4);
				qStatements[i][0] = statement1.getValue();
				qStatements[i][1] = statement2.getValue();
				qStatements[i][2] = statement3.getValue();
			}else{
				qStatements[i][0] = "&";
				qStatements[i][1] = "&";
				qStatements[i][2] = "&";
			}
		}
		return qStatements;
	}
	
	public void outString(){
		//  Debugging code
		for(int i = 0; i < numberOfQuestions; i++){
			System.out.println(questionTypeArray[i]);
			System.out.println(questionStatements[i]);
			System.out.println(questionAnswers[i]);
			System.out.println(questionOptions[i][0]);
			System.out.println(questionOptions[i][1]);
			System.out.println(questionOptions[i][2]);		
		}
	}
	/*
	public static void main(String[] args) {
		
		File f = new File("src/quiz5.xml");
		
		try {
			ParseTester parser = new ParseTester(f);
		//  Show on console
			parser.outString();
		} catch (IOException | JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	*/
}